#include <string>
#include <iostream>
#include <fstream>

#include "table.h"
extern std::string curDB;

table::table(titleNode* title, std::string name)  {
    tabletitle = title;
    rowNumber = 0;
    tableName = name;
    columnNumber = 0;

    titleNode* head = title;
    head = head->next;
    while(head != nullptr) {
      head = head->next;
      columnNumber++;
    }
}

table::table(std::string address) {
  tableRead(address);
}

void table::tableSave() {
  std::string address;
  address = "DATA/" + curDB + "/" + tableName + ".dat";
  
  std::ofstream out;
  out.open(address.c_str());

  // Table message;
  out << tableName 
    << std::endl << columnNumber 
    << std::endl << rowNumber;
  
  titleNode* head = tabletitle;
  head = head->next;
  while (head != nullptr) {
    out << std::endl << head->titleName
        << std::endl << head->titleType
        << std::endl << head->isPrimary;
    for(int i = 0; i < defaultLength; i++) {
      if (isFull[i]){
        out << std::endl;
        if (!head->titleType.compare("char")) out << head->strPtr[i];
        else out << head->intPtr[i];
      }
    }
    head = head->next;
  }

  out.close();
}

void table::tableRead(std::string address) {
  std::ifstream in;
  in.open(address.c_str());

  std::string con;
  in >> con; tableName = con;
  std::cout << tableName << std::endl;
  in >> columnNumber;
  std::cout << columnNumber << std::endl;
  in >> rowNumber;
  std::cout << rowNumber << std::endl;

  titleNode* head = (titleNode*)malloc(sizeof(titleNode));
  head->next = nullptr;
  tabletitle = head;

  for(int i = 0; i < columnNumber; i++) {
    titleNode *newColumn;
    if((newColumn = (titleNode*)malloc(sizeof(titleNode)))) std::cout << "success" << std::endl;
    newColumn->next = nullptr;

    in >> con; newColumn->titleName = con;///////////////////////////////////////////////////////////BUG!!!
    std::cout << con << std::endl;
    in >> con; newColumn->titleType = con;
    std::cout << con << std::endl;
    in >> newColumn->isPrimary;
    std::cout << newColumn->isPrimary << std::endl;
    if (!newColumn->titleType.compare("char")) {
      newColumn->strPtr = (std::string*)malloc(sizeof(std::string) * defaultLength);
      for(int i = 0; i < rowNumber; i++) {
        in >> con; newColumn->strPtr[i] = con;
        std::cout << con << std::endl;
        isFull[i] = 1;
      }
    } else {
      newColumn->intPtr = (int*)malloc(sizeof(int) * defaultLength);
      for (int i = 0; i < rowNumber; i++) {
        in >> con; newColumn->intPtr[i] = std::stoi(con);
        std::cout << con << std::endl;
        isFull[i] = 1;
      }
    }
    head->next = newColumn;
    head = head->next;
  }
 
  in.close();

}
/*
string read_from_file(char const *fileName)
{
    char content[256];
    string line;
    ifstream in;
    in.open(fileName);
    if(!in.is_open())
    {
        cout << "file not exit" << endl;
    }
    else
    {
        in.getline(content,256);
        line = content;
    }
    
    return line;
}
*/
/*
void generate_file_test(char const *fileName)
{
    ofstream out;
    out.open(fileName);
    // 判断文件是否已经打开
    if(out.is_open())
    {
        cout << "file created succeed" << endl;
        out.close();
        return true;
    }
    else
    {
        cout << "file created failed" << endl;
        out.close();
        return false;
    }
    
    out.close();
    return false;
}
*/